#include <iostream>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include<ctime>

#include "Yatzee.h"
#include "DieGenerator.h"

//Usings
using std::vector;
using std::cout;
using std::endl;
using std::max_element;

//Just the entry point.
//Pass off to main game interface.
int main(void)
    {
    bool GameExitResults;
    srand((unsigned) time(NULL));
    GameExitResults =    MainGameInterface ();

    if (GameExitResults == true)
            {
            return(EXIT_SUCCESS);
            }
    else
        return (EXIT_FAILURE);
    }

bool MainGameInterface ()
    {
    //First Roll the dice and get best results;
    for (int testloop = 0; testloop != 13; ++testloop)
            {
            vector<int> BestResults (7, 0);

            bool ReturnResults = GameRoll(BestResults);

            if (ReturnResults == true)
                    {
                    PrintRoll(BestResults, YATZEE);
                    }

            else
                    {
                    return false;
                    }
            }

    return true;
    }

//Return number of die to roll, sets strategy.  TODO: make strategy flex.
int YatzeeEvaluate (vector<int> &RollHolder, int CurrentDieRolls)
    {
    int  RollsToDo;
    vector<int>::iterator RollHolderIterator;

    //TODO: Current for top of card, make rest, like FH, straights.
    if (CurrentDieRolls > 10)
            {}

    //TODO detrmine threshold for above.
    //Hunt for 3 or more of a kind.
    //Find which die has largest count.
    int MaxValue = *max_element (RollHolder.begin(), RollHolder.end());
    int MaxRolled = MaxDieRolled (RollHolder, MaxValue);

    //Say if 3, then roll for YAT, else look for straight.  or other odds.
    int CountRollMax = RollHolder[MaxRolled];
    RollHolder.assign (7, 0);
    RollHolder[MaxRolled] = CountRollMax;
    RollsToDo = 5 - CountRollMax; //TODO not magic
#ifdef YATZBUG
    cout << "Max Value (" << MaxValue << ") in " << MaxRolled << ".  " << RollsToDo << " rolls left." << endl;
#endif
    return RollsToDo;

    }

//Roll the die to get max score.
bool GameRoll (vector<int> &GameRollResults)
    {

    int DieToCount = InitialDieToCount; //Set up for initial roll of all 5 die.

    DieGenerator six;
    vector<int>::iterator RollIterator;

    //Roll up to 3 times in order to get maximum possible score.
    for (int i = 0; i != MaxRollsPerTurn; ++i)
            {
            six.FillDieVector(DieToCount, DieSides);

            for (RollIterator = six.dice_vector.begin();  RollIterator < six.dice_vector.end(); ++RollIterator)
                    {
                    GameRollResults[*RollIterator]++;
#ifdef YATZBUG
                    cout << *RollIterator << "\t";
#endif
                    }

#ifdef YATZBUG
            cout << endl;
#endif
            //Evaluate, send back number of die to roll,
            DieToCount = YatzeeEvaluate (GameRollResults, DieToCount);

            if (DieToCount == 0)
                    {
                    return true;
                    }
            }

    return true;
    }

void PrintRoll (const vector<int> &ResultsToPrint, int Strategy)
    {

    int MaxValue = *max_element (ResultsToPrint.begin(), ResultsToPrint.end());
    int MaxDie = MaxDieRolled (ResultsToPrint, MaxValue);
    int Score = MaxDie * MaxValue;

    if (Strategy == YATZEE)
            {
            switch (MaxValue)
                    {
                    case
                            5:
                        cout << "YATZEE!  ";
                        Score = YATZEE;
                    }
            }

    cout << "You Rolled " << MaxValue << " of  " << MaxDie << ".  Your score is " << Score << endl << endl;
    //   switch (Strategy)
    //{
    //case YATZEE:
    // cout<<"YATZEE!"<<endl;
    // break;
    //   case TKIND:
    //   case FKIND:
    //default:
    //     cout<<"No Strategy!"<<endl;
    //}
    }

int Score (const vector<int> &GameRollResults)
    {
    int MaxValue = *max_element (GameRollResults.begin(), GameRollResults.end());
    int MaxDie = MaxDieRolled (GameRollResults, MaxValue);
    //ONES=1,
    //TWOS,
    //THREES,
    //FOURS,
    //FIVES,
    //SIXS,
    //TKIND,
    //FKIND,
    //CHANCE,
    //FHOUSE = 35,
    //STRAIGHT =30,
    //LSTRAIGHT=40,
    int RollScore = MaxValue * MaxDie;
    return RollScore;
    //YATZEE = 50
    }
